<?php

declare(strict_types=1);

namespace app\api\controller;

use app\common\model\Relation;
use app\common\model\Postulant;
use app\common\model\Expert;
use app\common\model\Assets;
use app\common\model\Message;
use app\common\model\Level;
use app\common\tool\Result;
use app\common\model\User as UserModel;
use app\common\model\Account;
use think\exception\ValidateException;


class User extends Base
{

    /**
     * 个人资料
     */
    public function userInfo()
    {
        try {
            $postulant = Postulant::where(['uid' => $this->user->id, 'status' => 2])->find();
            $assets = Assets::getFields(3);
            foreach ($assets as $k => $v) {
                $assetsdata['title'] = $v;
                $assetsdata['type'] = $k;
                $assetsdata['money'] = $this->user->assets->$k;
                $assetslist[] = $assetsdata;
            }
            $rule_url = Level::where('id', $this->user->level_id)->field('sub_title as name,url')->find();
            $Expert =  Expert::where(['uid' => $this->user->id])->where('type', '>=', 2)->find();

            $user = [
                'id' => $this->user->id,
                'code' => $this->user->code,
                'nickname' => $this->user->nickname,
                'photo' => $this->user->photo,
                'mobile' => encryptPhone($this->user->mobile),
                'parent_code' => $this->user->parent_code,
                'parent_name' => $this->user->parent_name,
                'level' => $this->user->level,
                'level_id' => $this->user->level_id,
                'job' => $this->user->job,
                'postulant' => empty($postulant) ? '' : ($postulant->status ==2 ? "爱心志愿者":""),
                'assets' => $assetslist,
                'rule_url' => $this->user->level_id > 1 ? $rule_url : [],
                'icon' => empty($Expert) ? '' : $Expert->icon,
                'rname' => empty($Expert) ? '' : $Expert->rname,
            ];
            return Result::success('获取成功！', $user);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 修改个人资料
     */
    public function edit()
    {
        try {
            $param = $this->request->post(['nickname', 'photo']);
            UserModel::update($param, ['id' => $this->user->id]);
            return Result::success('已修改');
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 我的团队
     */
    public function myTaem()
    {
        try {
            $page = $this->request->param('page', 1);
            $map = ['uid' => $this->user->id, 'algebra' => 1];
            $list = Relation::getPageList($map, 'new_uid', $page, 20, false, ['fresh']);
            foreach ($list['data'] as &$v) {
                $v->fresh->mobile = encryptPhone($v->fresh->mobile);
            }
            return Result::fastReturn($list);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 获取推广码
     */
    public function share()
    {
        try {
            return Result::fastReturn(UserModel::getShare($this->user));
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 爱心志愿者申请
     */
    public function postulantAdd()
    {
        try {
            $param = $this->request->post();
            $this->validate($param, 'Postulant');
            return Result::fastReturn(Postulant::add($param, $this->user));
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 爱心志愿者列表
     */
    public function getPostulantList()
    {
        try {
            $param = $this->request->get();
            $map['status'] = 2;
            $list =  Postulant::getPageList($map, 'id,images,rname,note,speciality,status', $param['page'], 20, false, '', '', [], [], [], '', ['status_text']);
            return Result::fastReturn($list);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 填写爱心志愿者详情
     */
    public function postulantDetails()
    {
        try {
            $param = $this->request->get();
            if (isset($param['id']) && $param['id']) {
                $map['id'] = $param['id'];
            } else {
                $map['uid'] = $this->user->id;
            }
            $info = Postulant::where($map)->find();
            if (empty($info)) {
                return Result::success('数据不存在！',['status'=>0]);
            }
            return Result::success('获取成功！', $info);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 投诉建议
     */
    public function suggest()
    {
        $param = $this->request->post();
        try {
            $this->validate($param, 'Message');
            return Result::fastReturn(Message::add($param, $this->user));
        } catch (ValidateException $e) {
            return Result::fail($e->getError());
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 账户明细
     */
    public function getAccountList()
    {
        try {
            $type = $this->request->get()['type'];
            if (!in_array($type, ['currency', 'integral', 'bonus'])) {
                return Result::fail('参数错误！');
            }
            $map = ['type' => $type, 'uid' => $this->user->id];
            $list = Account::getPageList($map, '*', $this->getPage(), 20, false, ['take']);
            $list['money'] = $this->user->assets->$type;
            return Result::fastReturn($list);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
}
